package lyx.tank.element
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import lyx.game.element.DynamicGameElement;
	import lyx.game.element.GameElement;
	import lyx.game.utils.Direction;
	import lyx.math.geometry.PanelGeometry;

	public class Tank extends DynamicGameElement
	{
		public var face_style:int = 0x0F9121;
		
		public var type:String = "Tank";
		
		public var _fire_interval:int = 250;
		private var _last_fire_time:int = 0;
		
		
		private var _graphics_face:Sprite = new Sprite();
		
		public function Tank(x:int=0,y:int=0,width:Number=40,height:Number=40)
		{
			super(x,y,width,height);	
		}
		
		public function fire():Bullet
		{
			var bullet:Bullet = null;
			var fire_time:int = getTimer();
			if((fire_time-_last_fire_time)>_fire_interval)
			{
				_last_fire_time = fire_time;
				
				var point:Point = PanelGeometry.rotatePoint(Math.PI*(_direction+2)/4,new Point(20,0),new Point(20,20));
				
				bullet = new Bullet(point.x+x,point.y+y,_direction,type);
				bullet.active_area = new Rectangle(0,0,active_area.width,active_area.height);
			}
			return bullet;
		}
		
		override protected function outActiveAreaHandler():void
		{
			moveRollBack();
		}
		
		override protected function updateViewContext():void
		{
			_graphics_face.graphics.clear();
			_graphics_face.graphics.beginFill(face_style);
			_graphics_face.graphics.drawRect(0,0,view_data.width,view_data.height);
			
			var point:Point = PanelGeometry.rotatePoint(Math.PI*(_direction+2)/4,new Point(20,5),new Point(20,20));
			
			_graphics_face.graphics.beginFill(0x888888);
			_graphics_face.graphics.drawCircle(point.x,point.y,5);
			_graphics_face.graphics.endFill();
			
			view_data.fillRect(view_data.rect,0);
			BitmapData(view_data).draw(_graphics_face);
		}
		
		//此处亦可重写render函数的渲染方式，而不一定采用_graphics_face -> view_data -> bitmap_data
//		override public function render(bitmap_data:BitmapData):void
//		{
//			updateViewContext();
//			bitmap_data.draw(_graphics_face,new Matrix(1,0,0,1,x,y));
//		}
	}
}